package com.wg.photo.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Went_Gone on 2017/5/18.
 */

public class FileUtil {
    private static List<String> paths = new ArrayList<>();

    private static BitmapFactory.Options getBitmapOption(int inSampleSize){
        System.gc();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPurgeable = true;
        options.inSampleSize = inSampleSize;
        return options;
    }

    public static Bitmap getBitmap(String filePath){
        return BitmapFactory.decodeFile(filePath,getBitmapOption(1));
    }

    public static String saveBitmapFile(File fileOld,String path,String fileName){
        String pathStr = path+"/"+fileName;
        Bitmap bitmap = ImageUtil.comp(fileOld);
        File file=new File(pathStr);//将要保存图片的路径
        pathStr = file.getAbsolutePath();
        paths.add(pathStr);
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return pathStr;
    }

    public void deleteBitmapFile(){
        for (int i = 0; i < paths.size(); i++) {
            File file = new File(paths.get(i));
            if (file.exists()){
                file.delete();
            }
        }
        paths.clear();
    }

  /*  public void saveImageBitmap(String path,String fileName){
        saveBitmapFile(ImageUtil.f);
    }*/
}
